<?php

namespace App\Http\Controllers\Admin;

use App\Models\BucketData;
use App\Models\Member;
use App\Models\MemberCash;
use App\Models\Order;
use App\Models\Shop;
use App\Models\ShopBucketCategory;
use App\Models\ShopGoods;
use App\Models\WaterBill;
use App\Models\WxTpl;
use App\Support\Printer;


class MemberController extends BaseController
{

    public function __construct(
        Member $member,
        MemberCash $memberCash,
        Shop $shop,
        BucketData $bucketData,
        ShopBucketCategory $shopBucketCategory,
        WaterBill $waterBill,
        ShopGoods $shopGoods,
        Order $order,
        WxTpl $wxTpl
    )
    {
        parent::__construct();
        $this->member = $member;
        $this->memberCash = $memberCash;
        $this->shop = $shop;
        $this->bucketData = $bucketData;
        $this->waterBill = $waterBill;
        $this->shopBucketCategory = $shopBucketCategory;
        $this->shopGoods = $shopGoods;
        $this->order = $order;
        $this->wxTpl = $wxTpl;
    }


    public function getIndex()
    {
        $rows = $this->member->getMemberList();
        $keyword = trim(request('keyword', ''));
        $type = $keyword ? request('type', 0) : 0;
        $shopInfo = [];
        return view('admin.single.member', compact('rows','keyword', 'type', 'shopInfo'));
    }

    public function getCash()
    {
        $shopList = $this->shop->_getList(['admin_id' => is_login()])->toArray();
        $rows = $this->memberCash->getList();
        if (request('shop_id')) {
            foreach ($shopList as $v) {
                if ($v['id'] == request('shop_id')) {
                    $curentShop = $v;
                }
            }
        }
        return view('admin.member.cash', compact('rows', 'shopList', 'curentShop'));
    }

    public function getInfo()
    {
        $info = $this->goods->_getOneById(request('id'));
        return view('admin.member.info', compact('info'));
    }

    /* 新增押金 */
    public function getAddCash()
    {
        $shop = $this->shop->_getOne(['id' => request('shop_id')]);
        $shop = $shop ? $shop->toArray() : [];
        $member = $this->member->_getOneById(request('id'));
        $bucketData = $this->bucketData->_getJionList('category', ['shop_id' => request('shop_id')])->toArray();
        return view('admin.member.cash', compact('shop', 'member', 'bucketData'));
    }


    public function postAddCash()
    {
        $info = $this->memberCash->_getOne(['mid' => request('id'), 'status' => 0]);
        if ($info) {
            return $this->error('非法操作');
        }
        $res = $this->memberCash->addOrUpdate(request('shop_id'));
        if (!$res) {
            return $this->error($this->memberCash->getError() ? $this->memberCash->getError() : '未知错误');
        }
        return $this->success('添加押金成功', 'javascript:history.back(-2);');
    }

    /* 老用户导入 */
    public function getImport()
    {
        $shop = $this->shop->_getOne(['id' => request('shop_id')]);
        $shop = $shop ? $shop->toArray() : [];
        $bucketData = $this->bucketData->_getJionList('category', ['shop_id' => request('shop_id')])->toArray();
        if (!$bucketData) {
            return $this->error('请先添加店铺分类');
        }
        $waterBill = $this->waterBill->_getList(['shop_id' => request('shop_id'), 'status' => 1])->toArray();
        if (!$waterBill) {
            return $this->error('请先添加水票');
        }
        $member = $this->member->_getOneById(request('id'));
        return view('admin.member.import', compact('shop', 'bucketData', 'waterBill', 'member'));
    }

    public function postImport()
    {
        $res = $this->member->import(request('shop_id'), request('admin_id'));
        if (!$res) {
            return $this->error($this->member->getError() ? $this->member->getError() : '未知错误');
        }
        return $this->success('导入成功');
    }

    /* 老用户导入 */
    public function getBackCash()
    {
        $info = $this->memberCash->_getOne(['id' => request('id'), 'status' => 0]);
        if (!$info) {
            return $this->error('非法操作');
        }
        $shop = $this->shop->_getOne(['id' => request('shop_id')]);
        $shop = $shop ? $shop->toArray() : [];
        $member = $this->member->_getOneById(request('mid'));
        $bucketData = $this->bucketData->_getJionList('category', ['shop_id' => request('shop_id')])->toArray();
        return view('admin.member.cash', compact('shop', 'member', 'bucketData', 'info'));
    }

    public function postBackCash()
    {
        $res = $this->memberCash->addOrUpdate(request('shop_id'));
        if (!$res) {
            return $this->error($this->memberCash->getError() ? $this->memberCash->getError() : '未知错误');
        }
        return $this->success('操作成功', 'javascript:history.back(-2);');
    }

    /* 水票充值 */
    public function getCharge()
    {
        $shop = $this->shop->_getOne(['id' => request('shop_id')]);
        $shop = $shop ? $shop->toArray() : [];
        $waterBill = $this->waterBill->_getList(['shop_id' => request('shop_id'), 'status' => 1])->toArray();
        if (!$waterBill) {
            return $this->error('请先添加水票');
        }
        $member = $this->member->_getOneById(request('id'));
        return view('admin.single.memberl_charge', compact('shop', 'waterBill', 'member'));
    }

    public function postCharge()
    {
        $res = $this->member->charge(request('bill_id'), request('waterbill_num'), request('id'), request('shop_id'), request('money'));
        if (!$res) {
            return $this->error($this->member->getError() ? $this->member->getError() : '未知错误');
        }
        return $this->success('充值成功', 'javascript:history.back(-2);');
    }

    public function anySetStatus()
    {
        $ids = is_array(request('ids')) ? request('ids') : [request('ids')];
        $xcx_openid = request('xcx_openid');
        $shop_id = request('shop_id');
        $status = request('status');
        if (empty($ids)) {
            return $this->error('请选择要操作的数据');
        }
        $where['id'] = ['in', $ids];
        switch ($status) {
            case 0  :
                $this->member->_redisGenerateByOpenId($ids, $shop_id);
                return $this->forbid($this->member, $where, ['success' => '禁用成功', 'error' => '禁用失败']);
            case 1  :
                $this->member->_redisGenerateByOpenId($ids, $shop_id);
                return $this->resume($this->member, $where, ['success' => '启用成功', 'error' => '启用失败']);
            default :
                return $this->error('参数错误');
        }
    }

    public function getAdd()
    {
        $shopList = $this->shop->_getList(['admin_id' => is_login()])->toArray();
        return view('admin.member.member_edit', compact('shopList'));
    }

    public function postAdd()
    {
        $res = $this->member->addOrUpdate(session('admin.id'), request('shop_id'));
        if (!$res) {
            return $this->error($this->member->getError() ? $this->member->getError() : '未知错误');
        }
        return $this->success('新增成功', 'javascript:history.back(-2);');
    }

    public function getEdit()
    {
        $shopList = $this->shop->_getList(['admin_id' => is_login()])->toArray();
        $memberFields = 'id,name,mobile,type,shop_id,company_name,area_info,address,city_id,area_id';
        $addressFields = 'id,address,mid';
        $info = $this->member->_getJionOne('address',['id'=>request('id')],$memberFields,$addressFields,['is_default'=>1]);
        if (!$info) {
            return $this->error('非法操作');
        }
        $info = $info->toArray();
        return view('admin.member.member_edit', compact('shopList','info'));
    }

    public function postEdit()
    {
        $res = $this->member->addOrUpdate(session('admin.id'), request('shop_id'));
        if (!$res) {
            return $this->error($this->member->getError() ? $this->member->getError() : '未知错误');
        }
        return $this->success('编辑成功', 'javascript:history.back(-2);');
    }

    public function getAddOrder()
    {
        $shopId = request('shop_id');
        $shop = $this->shop->_redisGetOneBySid($shopId);
        if (!(request('id') && $shopId)) {
            return $this->error('非法操作');
        }
        $member = $this->member->_getJionOne('address',['id'=>request('id')], 'id,name,shop_id,admin_id,worker_id','mid,mobile,area_info,address',['is_default'=>1]);
        $goodsList = $this->shopGoods->_getList(['shop_id' => $shopId, 'status' => 1], 'id,goods_name')->toArray();
        return view('admin.member.add_order', compact('shop', 'member', 'goodsList'));
    }

    public function postAddOrder()
    {
        $goodsId = request('goods_id');
        $mid = request('mid');
        $shopId = request('shop_id');
        $adminId = request('admin_id');
        $goodsNum = request('goods_num');
        $money = request('money');
        $orderData = $this->order->creatOrder($mid, $shopId, $adminId, $goodsId, $goodsNum, $money);
        if (!$orderData) {
            return $this->error($this->order->getError() ? $this->order->getError() : '未知错误');
        }
        $this->wxTpl->newOrderMsg($orderData, $shopId);
        (new Printer())->typeSetting($orderData);
        return $this->success('下单成功', urlAdmin('order', 'getIndex'));
    }

    public function getMove()
    {
        $shopId = request('shop_id');
        $shop = $this->shop->_redisGetOneBySid($shopId);
        if (!(request('id') && $shopId)) {
            return $this->error('非法操作');
        }
        $member = $this->member->_getOneById(request('id'), 'id,name,shop_id,admin_id,mobile,area_info,address,company_name');
        return view('admin.member.move', compact('shop', 'member'));
    }

    public function postMove()
    {
        $res = $this->member->yijiao(request('mid'), request('move_id'), request('admin_id'), request('shop_id'));
        if (!$res) {
            return $this->error($this->member->getError() ? $this->member->getError() : '未知错误');
        }
        return $this->success('移交成功', '', true);
    }

}
